import { Button, Checkbox, Form, Input, Col, message } from "antd";
import "./index.scss";
import PageName from "../../../../../components/PageName/index.jsx";
import { useNavigate, useLocation } from "react-router-dom";
import {
  checkOneAuthority,
  EditAuthority,
} from "../../../../../utils/httpAllMethods/AuthHttp.jsx";
import { useEffect } from "react";
const onFinishFailed = (errorInfo) => {
};
const AddAuthority = () => {
  const id = useLocation().state.id;
  const navigator = useNavigate();
  const [form] = Form.useForm();
  const onSearchMsg = (value) => {
    let authAll = [
      value.floor,
      value.require,
      value.content,
      value.consult,
      value.activity,
      value.web,
      value.user,
      value.params,
      value.auth,
    ];
    let admin_authority= authAll.flat().filter((value) => value !== undefined);
    EditAuthority({
      // id
      admin_id:id,// 账号
      admin_name: value.admin_name, // 姓名
      admin_nickname:value.admin_nickname, // 手机号
      admin_tel:value.admin_tel, // 授权城市
      admin_city:value.admin_city.join(), // 权限
      admin_authority:admin_authority.join(),
    }).then((res)=>{
      message.success('修改成功!')
      navigator('/home/Authority')
    });
  };
  const quit = () => {
    navigator("/home/Authority");
  };
  useEffect(() => {
    checkOneAuthority({ admin_id: id }).then((res) => {
      let authData = res.data[0];
      form.setFieldsValue({
        admin_name: authData.admin_name,
        admin_nickname: authData.admin_nickname,
        admin_tel: authData.admin_tel,
        admin_city: authData.admin_city.split(","),
        floor: authData.admin_authority
          .split(",")
          .filter((element) => element == "1"),
        require: authData.admin_authority
          .split(",")
          .filter((element) => element == "2"),
        content: authData.admin_authority
          .split(",")
          .filter(
            (element) =>
              element == "3" ||
              element == "4" ||
              element == "5" ||
              element == "6"
          ),
        consult: authData.admin_authority
          .split(",")
          .filter((element) => element == "7"),
        activity: authData.admin_authority
          .split(",")
          .filter((element) => element == "8"),
        web: authData.admin_authority
          .split(",")
          .filter((element) => element == "9" || element == "10"),
        user: authData.admin_authority
          .split(",")
          .filter((element) => element == "11" || element == "12"),
        params: authData.admin_authority
          .split(",")
          .filter(
            (element) => element == "13" || element == "14" || element == "15"
          ),
        auth: authData.admin_authority
          .split(",")
          .filter((element) => element == "16"),
      });
    });
  }, []);
  return (
    <>
      <PageName name="添加用户"></PageName>
      <Form
        style={{ marginTop: "20px" }}
        colon={false}
        onFinish={onSearchMsg}
        form={form}
        name="basic"
        labelCol={{
          span: 2,
        }}
        wrapperCol={{
          span: 6,
        }}
        initialValues={{
          remember: true,
        }}
        onFinishFailed={onFinishFailed}
        autoComplete="off"
      >
        <Form.Item
          label="账号"
          name="admin_name"
          labelAlign="right"
          wrapperCol={{
            span: 6,
          }}
          rules={[
            {
              required: true,
              message: "Please input your username!",
            },
          ]}
        >
          <Input></Input>
        </Form.Item>

        <Form.Item
          label="姓名"
          name="admin_nickname"
          wrapperCol={{
            span: 6,
          }}
          rules={[
            {
              required: true,
              message: "Please input your password!",
            },
          ]}
        >
          <Input />
        </Form.Item>

        <Form.Item
          label="手机号"
          name="admin_tel"
          wrapperCol={{
            span: 6,
          }}
          rules={[
            {
              required: true,
              message: "Please input your password!",
            },
          ]}
        >
          <Input />
        </Form.Item>

        <p className="cityAuth">授权城市</p>

        <Form.Item
          label=" "
          name="admin_city"
          labelCol={{
            span: 1,
          }}
          wrapperCol={{
            span: 6,
          }}
        >
          <Checkbox.Group>
            <Checkbox
              value="北京"
              style={{
                lineHeight: "32px",
              }}
            >
              北京
            </Checkbox>
            <Checkbox
              value="上海"
              style={{
                lineHeight: "32px",
              }}
            >
              上海
            </Checkbox>
            <Checkbox
              value="广州"
              style={{
                lineHeight: "32px",
              }}
            >
              广州
            </Checkbox>
            <Checkbox
              value="深圳"
              style={{
                lineHeight: "32px",
              }}
            >
              深圳
            </Checkbox>
          </Checkbox.Group>
        </Form.Item>
        <p className="cityAuth">权限分配</p>
        <Form.Item label="楼盘管理" name="floor">
          <Checkbox.Group>
            <Checkbox
              value="1"
              style={{
                lineHeight: "32px",
              }}
            >
              新房
            </Checkbox>
          </Checkbox.Group>
        </Form.Item>
        <Form.Item label="需求管理" name="require">
          <Checkbox.Group>
            <Checkbox
              value="2"
              style={{
                lineHeight: "32px",
              }}
            >
              需求管理
            </Checkbox>
          </Checkbox.Group>
        </Form.Item>
        <Form.Item label="内容审核" name="content">
          <Checkbox.Group>
            <Checkbox
              value="3"
              style={{
                lineHeight: "32px",
                width: "130px",
              }}
            >
              楼盘点评
            </Checkbox>
            <Checkbox
              value="4"
              style={{
                lineHeight: "32px",
                width: "130px",
              }}
            >
              楼盘解读
            </Checkbox>
            <Checkbox
              value="5"
              style={{
                lineHeight: "32px",
                width: "130px",
              }}
            >
              经纪人点评
            </Checkbox>
            <Checkbox
              value="6"
              style={{
                lineHeight: "32px",
                width: "130px",
              }}
            >
              服务案例
            </Checkbox>
          </Checkbox.Group>
        </Form.Item>
        <Form.Item label="咨询管理" name="consult">
          <Checkbox.Group>
            <Checkbox
              value="7"
              style={{
                lineHeight: "32px",
              }}
            >
              行业资讯
            </Checkbox>
          </Checkbox.Group>
        </Form.Item>
        <Form.Item label="活动管理" name="activity">
          <Checkbox.Group>
            <Checkbox
              value="8"
              style={{
                lineHeight: "32px",
              }}
            >
              红包活动
            </Checkbox>
          </Checkbox.Group>
        </Form.Item>
        <Form.Item label="网站管理" name="web">
          <Checkbox.Group>
            <Checkbox
              value="9"
              style={{
                lineHeight: "32px",
              }}
            >
              焦点图
            </Checkbox>
            <Checkbox
              value="10"
              style={{
                lineHeight: "32px",
              }}
            >
              广告
            </Checkbox>
          </Checkbox.Group>
        </Form.Item>
        <Form.Item label="用户管理" name="user">
          <Checkbox.Group>
            <Checkbox
              value="11"
              style={{
                lineHeight: "32px",
              }}
            >
              个人用户
            </Checkbox>
            <Checkbox
              value="12"
              style={{
                lineHeight: "32px",
              }}
            >
              经纪人
            </Checkbox>
          </Checkbox.Group>
        </Form.Item>
        <Form.Item label="参数配置" name="params">
          <Checkbox.Group>
            <Checkbox
              value="13"
              style={{
                lineHeight: "32px",
              }}
            >
              城市区域
            </Checkbox>
            <Checkbox
              value="14"
              style={{
                lineHeight: "32px",
              }}
            >
              轨道交通
            </Checkbox>
            <Checkbox
              value="15"
              style={{
                lineHeight: "32px",
              }}
            >
              项目特色
            </Checkbox>
          </Checkbox.Group>
        </Form.Item>
        <Form.Item label="权限管理" name="auth">
          <Checkbox.Group>
            <Checkbox
              value="16"
              style={{
                lineHeight: "32px",
              }}
            >
              权限
            </Checkbox>
          </Checkbox.Group>
        </Form.Item>

        <div className="btn-all">
          <Button type="primary" htmlType="submit" className="btn">
            确认
          </Button>
          <Button className="btn" onClick={quit}>
            取消
          </Button>
        </div>
      </Form>
    </>
  );
};
export default AddAuthority;
